#!/usr/bin/env python
# coding: utf-8

# In[2]:


import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
import seaborn as sns
import matplotlib.dates as mdates
import numpy as np # linear algebra
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
import math           
from datetime import datetime, date 
import warnings
warnings.filterwarnings('ignore')
from tensorflow import keras
from tensorflow.keras import layers
from keras.models import Sequential
from keras.layers import Dense

print("Setup Complete")


# # Fig4

# In[9]:


dam = pd.read_csv("result_nse_kge.csv", encoding= 'cp949')
dam


# In[10]:


dam.columns = ['dam_name', 'NSE of Train Period','KGE of Train Period', 'NSE of Test Period', 'KGE of Test Period']
dam.head()


# In[18]:


plt.figure(figsize=(33, 15))

ax = plt.gca()  

bar_width = 0.2
spacing = 0.02  

x1 = range(len(dam['dam_name']))
x2 = [x + bar_width + spacing for x in x1]
x3 = [x + bar_width + spacing for x in x2]
x4 = [x + bar_width + spacing for x in x3]

ax.bar(x1, dam['NSE of Train Period'], width=bar_width, label='NSE of Train Period', color='g', alpha=0.7, edgecolor='black', linewidth=2, hatch='//')
ax.bar(x2, dam['KGE of Train Period'], width=bar_width, label='KGE of Train Period', color='greenyellow', alpha=0.7, edgecolor='black', linewidth=2, hatch='\\\\')
ax.bar(x3, dam['NSE of Test Period'], width=bar_width, label='NSE of Test Period', color='r', alpha=0.7, edgecolor='black', linewidth=2, hatch='--')
ax.bar(x4, dam['KGE of Test Period'], width=bar_width, label='KGE of Test Period', color='orange', alpha=0.7, edgecolor='black', linewidth=2, hatch='||')

plt.xticks([x + bar_width for x in x2], dam['dam_name'], fontsize=50)
plt.yticks(fontsize=50)

plt.ylabel('NSE', fontsize=60)
plt.ylim(0,1.1)

ax.spines['bottom'].set_linewidth(2)
ax.spines['top'].set_linewidth(2)
ax.spines['left'].set_linewidth(2)
ax.spines['right'].set_linewidth(2)

plt.legend(fontsize=40, loc=(-0.1, -0.2), edgecolor='black', ncol=4)

plt.tight_layout()
plt.savefig('nse_kge_result.png', dpi=300)
plt.show()


# # Fig3

# In[130]:


dam = pd.read_csv("nse_kge_moving_average.csv", encoding= 'cp949')
dam


# In[131]:


dam.index=dam.index + 1


# In[132]:


A=pd.DataFrame(dam['A_seomjin'])
B=pd.DataFrame(dam['A_juam_control'])
C=pd.DataFrame(dam['A_juam'])
D=pd.DataFrame(dam['B_seomjin'])
E=pd.DataFrame(dam['B_juam_control'])
F=pd.DataFrame(dam['B_juam'])
G=pd.DataFrame(dam['C_seomjin'])
H=pd.DataFrame(dam['C_juam_control'])
I=pd.DataFrame(dam['C_juam'])


# In[133]:


fig = plt.figure(figsize=(15, 8))
ax = fig.add_subplot(1,1,1) 

ax.plot(A, label='Seomjin River Dam',linewidth=3, color='r')

ax.set_ylabel('NSE', fontsize=35) 
ax.set_xlabel('Days for Moving Average', fontsize=30) 
ax.yaxis.set_major_formatter(plt.FormatStrFormatter('%.3f'))

ax.spines['bottom'].set_linewidth(2)
ax.spines['top'].set_linewidth(2)
ax.spines['left'].set_linewidth(2)
ax.spines['right'].set_linewidth(2)

plt.xticks(range(len(A)), fontsize=27)  
plt.yticks(fontsize =28)
plt.grid(True)

ax.axvline(x=6, color='red', linewidth=3, linestyle='dotted')

ax.legend(fontsize=26, loc='best', edgecolor='black')
plt.savefig('ma_nse_seomjin.png', dpi=300) 
plt.show()


# In[134]:


fig = plt.figure(figsize=(15, 8)) 
ax = fig.add_subplot(1,1,1) 

ax.plot(C, label='Juam Dam',linewidth=3, color='g') 
ax.plot(B, label='Juam Control Dam',linewidth=3, color='b') 

ax.set_ylabel('NSE', fontsize=35) 
ax.set_xlabel('Days for Moving Average', fontsize=30) 

ax.yaxis.set_major_formatter(plt.FormatStrFormatter('%.3f'))

ax.spines['bottom'].set_linewidth(2)
ax.spines['top'].set_linewidth(2)
ax.spines['left'].set_linewidth(2)
ax.spines['right'].set_linewidth(2)

plt.xticks(range(len(A)), fontsize=27)  
plt.yticks(fontsize =28)
plt.grid(True)

ax.axvline(x=2, color='green', linewidth=3, linestyle='dotted')

ax.axvline(x=11, color='blue', linewidth=3, linestyle='dotted')

ax.legend(fontsize=26, loc='best', edgecolor='black') 
plt.savefig('ma_nse_juam_jc.png', dpi=300)
plt.show()


# # Fig6

# In[12]:


dam = pd.read_csv("seomjin_test.csv", encoding= 'cp949')
dam.head()


# In[13]:


dam.columns = ['date', 'water_level', 'pre', 'rainfall','outflow']
dam.head()


# In[14]:


dam.describe()


# In[15]:


dam = dam.reset_index(drop=True)


# In[16]:


dam["date"] = pd.to_datetime(dam["date"])
dam.index = dam["date"]


# In[17]:


A=pd.DataFrame(dam['water_level'])
B=pd.DataFrame(dam['pre'])


# In[206]:


plt.rcParams['figure.figsize'] = (29, 11)
plt.rcParams['font.size'] = 18

fig, ax1 = plt.subplots()
ax1.plot(A, label='Obs', color='black', linewidth=5) 
ax1.plot(B, label='GRU', color='r', linestyle='--', linewidth=5) 
ax1.set_ylabel('Water Level (EL.m)', fontsize=47)
ax1.tick_params(axis='y', direction='in', labelsize=40)
ax1.tick_params(axis='x', labelsize=35) 
ax1.tick_params(axis='y', labelsize=37)

ax1.axhline(y=197.7, color='r', linestyle='--', linewidth=3.5) 
ax1.axhline(y=196.5, color='g', linestyle='--', linewidth=3.5) 
ax1.axhline(y=194, color='b', linestyle='--', linewidth=3.5) 
ax1.grid(True)

ax2 = ax1.twinx()
ax2.plot(dam.rainfall, label='Rainfall', color='b', linewidth=5)
ax2.set_ylabel('Rainfall (mm/hr)', color='b', fontsize=47)
ax2.tick_params(axis='y', colors='blue', labelsize=40)

ax1.set_title('Seomjin river dam\n', fontsize=28)

ax1.set_zorder(ax1.get_zorder() + 10)
ax1.patch.set_visible(False)

ax1.spines['bottom'].set_linewidth(2)
ax1.spines['top'].set_linewidth(2)
ax1.spines['left'].set_linewidth(2)
ax1.spines['right'].set_linewidth(2)

# Get legend handles and labels from both axes
handles1, labels1 = ax1.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()

# Combine the handles and labels from both axes
all_handles = handles1 + handles2
all_labels = labels1 + labels2

# Create a single legend using the combined handles and labels
legend = ax1.legend(all_handles, all_labels, fontsize=36, loc='upper left', edgecolor='black')
for legobj in legend.legendHandles:
    legobj.set_alpha(1)  

plt.savefig('fig6_seomjin.png', dpi=300) 

plt.show()


# In[18]:


dam = pd.read_csv("juam_test.csv", encoding= 'cp949')
dam.head()


# In[19]:


dam.columns = ['date', 'water_level', 'pre', 'rainfall','outflow']
dam.head()


# In[20]:


dam = dam.reset_index(drop=True)


# In[21]:


dam["date"] = pd.to_datetime(dam["date"])


# In[22]:


dam.index = dam["date"]


# In[23]:


A=pd.DataFrame(dam['water_level'])
B=pd.DataFrame(dam['pre'])


# In[197]:


plt.rcParams['figure.figsize'] = (29, 11)
plt.rcParams['font.size'] = 18

fig, ax1 = plt.subplots()
ax1.plot(A, label='Obs', color='black', linewidth=5) 
ax1.plot(B, label='GRU', color='r', linestyle='--', linewidth=5) 
ax1.set_ylabel('Water Level (EL.m)', fontsize=47)
ax1.tick_params(axis='y', direction='in', labelsize=40)
ax1.tick_params(axis='x', labelsize=35) 
ax1.tick_params(axis='y', labelsize=37)

ax1.axhline(y=108.5, color='g', linestyle='--', linewidth=3.5) 
ax1.grid(True)

ax1.set_title('Juam dam\n', fontsize=28)

ax2 = ax1.twinx()
ax2.plot(dam.rainfall, label='Rainfall', color='b', linewidth=5)
ax2.set_ylabel('Rainfall (mm/hr)', color='b', fontsize=47)
ax2.tick_params(axis='y', colors='blue', labelsize=40)

ax1.set_zorder(ax1.get_zorder() + 10)
ax1.patch.set_visible(False)

ax1.spines['bottom'].set_linewidth(2)
ax1.spines['top'].set_linewidth(2)
ax1.spines['left'].set_linewidth(2)
ax1.spines['right'].set_linewidth(2)

# Get legend handles and labels from both axes
handles1, labels1 = ax1.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()

# Combine the handles and labels from both axes
all_handles = handles1 + handles2
all_labels = labels1 + labels2

# Create a single legend using the combined handles and labels
legend = ax1.legend(all_handles, all_labels, fontsize=36, loc='upper left', edgecolor='black')
for legobj in legend.legendHandles:
    legobj.set_alpha(1)  
    
plt.savefig('fig6_juam.png', dpi=300) 

plt.show()


# In[24]:


dam = pd.read_csv("juam_control_test.csv", encoding= 'cp949')
dam.head()


# In[25]:


dam.columns = ['date', 'water_level', 'pre', 'rainfall','outflow']
dam.head()


# In[26]:


dam = dam.reset_index(drop=True)


# In[27]:


dam["date"] = pd.to_datetime(dam["date"])


# In[28]:


dam.index = dam["date"]


# In[29]:


A=pd.DataFrame(dam['water_level'])
B=pd.DataFrame(dam['pre'])


# In[188]:


plt.rcParams['figure.figsize'] = (29, 11)
plt.rcParams['font.size'] = 18

fig, ax1 = plt.subplots()
ax1.plot(A, label='Obs', color='black', linewidth=5) 
ax1.plot(B, label='GRU', color='r', linestyle='--', linewidth=5) 
ax1.set_ylabel('Water Level (EL.m)', fontsize=47)
ax1.tick_params(axis='y', direction='in', labelsize=40)
ax1.tick_params(axis='x', labelsize=35) 
ax1.tick_params(axis='y', labelsize=37)

ax1.axhline(y=108.5, color='g', linestyle='--', linewidth=3.5) 
ax1.grid(True)

ax2 = ax1.twinx()
ax2.plot(dam.rainfall, label='Rainfall', color='b', linewidth=5)
ax2.set_ylabel('Rainfall (mm/hr)', color='b', fontsize=47)
ax2.tick_params(axis='y', labelsize=40, colors='blue')

ax1.set_title('Juam Control dam\n', fontsize=28)

ax1.set_zorder(ax1.get_zorder() + 10)
ax1.patch.set_visible(False)

ax1.spines['bottom'].set_linewidth(2)
ax1.spines['top'].set_linewidth(2)
ax1.spines['left'].set_linewidth(2)
ax1.spines['right'].set_linewidth(2)

# Get legend handles and labels from both axes
handles1, labels1 = ax1.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()

# Combine the handles and labels from both axes
all_handles = handles1 + handles2
all_labels = labels1 + labels2

# Create a single legend using the combined handles and labels
legend = ax1.legend(all_handles, all_labels, fontsize=36, loc='upper left', edgecolor='black')
for legobj in legend.legendHandles:
    legobj.set_alpha(1)  

plt.savefig('fig6_juam_control.png', dpi=300) 

plt.show()


# In[ ]:




